//动态菜单数据
//只用于渲染当前用户页面
export const allMenus = [
  {
    id: "1",
    title: "首页",
    path: "/Admin/Home",
  },
  {
    id: "2",
    title: "轮播图管理",
    children: [
      {
        id: "2-1",
        title: "轮播图列表",
        path: "/Admin/Banner",
      },
    ],
  },
  {
    id: "3",
    title: "操作人员管理",
    children: [
      {
        id: "3-1",
        title: "操作人员列表",
        path: "/Admin/Operator",
      },
    ],
  },
  {
    id: "4",
    title: "秒杀列表",
    children: [
      {
        id: "4-1",
        title: "秒杀列表",
        path: "/Admin/Operatorone",
      },
      {
        id: "4-2",
        title: "推荐列表",
        path: "/Admin/recommend",
      },
      {
        id: "4-3",
        title: "筛选列表",
        path: "/Admin/search",
      },
      {
        id: "4-4",
        title: "哈哈哈列表",
        path: "/Admin/productlist",
      },
    ],
  },
  {
    id: "5",
    title: "地图",
    path: "/Admin/Ditu",
  },
  {
    id: "6",
    title: "柱状图",
    path: "/Admin/Curve",
  },
];

//根据权限数组 checkedkeys  过滤出对应的菜单

export const filterMenus = () => {
  //获取当前用户权限(checkedkeys)
  let checkedKeys = JSON.parse(sessionStorage.getItem("checkedkeys") || "[]");
  //如果没有任何权限，则添加操作人员的数据
  if (checkedKeys.length === 0) {
    checkedKeys = ["3", "3-1"];
  }

  // 定义数组->为过滤数据存放
  const currMenus: any = [];
  //循环allMenus
  allMenus.forEach((item) => {
    //item为5个对象遍历每一个对象
    //判断每个对象没的id是否在用户权限中存在
    if (checkedKeys.includes(item.id)) {
      //如果存在   定义数组存放将筛选出来存在的数据定义为一个新对象
      const newItem = {
        id: item.id,
        title: item.title,
        path: item.path,
      };
      //将新对象push到定义的数组内，
      currMenus.push(newItem);
      //再判断如果有这个对象有children
      if (item.children) {
        // 创建newItem的children为一个对象
        newItem.children = [];
        //再循环children，children为children下的每一个对象
        item.children.forEach((childItem) => {
          //判断子级的id是否在checkedkeys（用户权限）中存在
          if (checkedKeys.includes(childItem.id)) {
            //那么将这个数据定义push到newItem的子级对象中
            newItem.children.push({
              id: childItem.id,
              title: childItem.title,
              path: childItem.path,
            });
          }
        });
      }
    }
  });
  return currMenus;
};
